<?php
/**
 * export all member records in .xls format
 * with the help of the xlshelper
 */

	function xlsbof() {
		echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
	}

	function xlseof() {
		echo pack("ss", 0x0a, 0x00);
	}
	function xlswritenumber($row, $col, $value) {
		echo pack("sssss", 0x203, 14, $row, $col, 0x0);
		echo pack("d", $value);
	}
	function xlswritelabel($row, $col, $value) {
		$l = strlen($value);
		echo pack("ssssss", 0x204, 8 + $l, $row, $col, 0x0, $l);
		echo $value;
	}
	
	function getAccentedText($input) {
	
		return iconv('UTF-8', 'ISO-8859-1', $input);
	}
	
	// prepare headers information
	header("content-type: application/force-download");
	header("content-type: application/octet-stream");
	header("content-type: application/download");
	header("content-disposition: attachment; filename=\"export_".date("d-m-y").".xls\"");
	header("content-transfer-encoding: binary");
	header("pragma: no-cache");
	header("expires: 0");
	// start exporting
	xlsbof();
	// first row
	xlswritelabel(0, 0, getAccentedText("RELATÓRIO CLIENTES CADASTRADOS"));
	
	xlswritelabel(3, 0, getAccentedText("NOME"));
	xlswritelabel(3, 1, getAccentedText("CPF"));
	xlswritelabel(3, 2, getAccentedText("VIDAS"));
	xlswritelabel(3, 3, getAccentedText("PLANO"));
	xlswritelabel(3, 4, getAccentedText("SAÚDE"));
	xlswritelabel(3, 5, getAccentedText("ODONT"));
	xlswritelabel(3, 6, getAccentedText("SEGURO"));
	xlswritelabel(3, 7, getAccentedText("UF"));
	xlswritelabel(3, 8, getAccentedText("PAGAMENTO"));
	xlswritelabel(3, 9, getAccentedText("STATUS"));
	// second row
	//xlswritenumber(4, 0, sizeof($clientes));
	/*xlswritelabel(4, 1, "john");
	xlswritelabel(4, 2, "john@yahoo.com");
	// third row
	xlswritenumber(5, 0, 350);
	xlswritelabel(5, 1, "mark");
	xlswritelabel(5, 2, "mark@yahoo.com");*/
	
	for($i = 0; $i < sizeof($clientes); $i++) {
		xlswritelabel($i+4, 0, getAccentedText($clientes[$i]['Cliente']['nome']));
		xlswritelabel($i+4, 1, getAccentedText($clientes[$i]['Cliente']['cpf']));
		xlswritenumber($i+4, 2, getAccentedText($clientes[$i]['Cliente']['rg']));
		xlswritelabel($i+4, 3, getAccentedText($clientes[$i]['Cliente']['cep']));
		xlswritelabel($i+4, 4, getAccentedText($clientes[$i]['Cliente']['bairro']));
		xlswritelabel($i+4, 5, getAccentedText($clientes[$i]['Cliente']['cidade']));
		xlswritelabel($i+4, 6, getAccentedText($clientes[$i]['Cliente']['nomemae']));
		xlswritelabel($i+4, 7, getAccentedText($clientes[$i]['Uf']['abreviacao']));
		xlswritelabel($i+4, 8, getAccentedText($clientes[$i]['Fpagamento']['nome']));
		xlswritelabel($i+4, 9, getAccentedText($clientes[$i]['Cliente']['email']));
	}
	// end exporting
	xlseof();
?>